# encoding: UTF-8

class PhieuNhapsController < ApplicationController

  # GET /params
  # GET /params.json
  def index
    @phieu_nhaps = PhieuNhap.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @phieu_nhaps }
    end
  end

  # GET /phieu_nhaps/1
  # GET /phieu_nhaps/1.json
  def show
    @phieu_nhap = PhieuNhap.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @phieu_nhap }
    end
  end

  # GET /phieu_nhaps/new
  # GET /phieu_nhaps/new.json
  def new
    if sign_in?
      @phieu_nhap = PhieuNhap.new

      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @phieu_nhap }
      end
    else
      render 'sessions/deny'
    end

  end

  # GET /phieu_nhaps/1/edit
  def edit
    if sign_in?
    @phieu_nhap = PhieuNhap.find(params[:id])
    else
      render 'sessions/deny'
    end
  end

  # POST /phieu_nhaps
  # POST /phieu_nhaps.json
  def create
    @phieu_nhap = PhieuNhap.new(params[:phieu_nhap])

    respond_to do |format|
      if @phieu_nhap.save

        ts = TaiSan.find(@phieu_nhap.tai_san_id)
        ts.SLuong = ts.SLuong.to_i + @phieu_nhap.so_luong.to_i
        ts.save!

        format.html { redirect_to '/tai_sans/' + @phieu_nhap.tai_san_id.to_s, notice: 'Lưu thành công!!!' }
        format.json { render json: @phieu_nhap, status: :created, location: @phieu_nhap }
      else
        format.html { render action: "new" }
        format.json { render json: @phieu_nhap.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /phieu_nhaps/1
  # PUT /phieu_nhaps/1.json
  def update
    @phieu_nhap = PhieuNhap.find(params[:id])
    @home = "/tai_sans"
    respond_to do |format|
      if @phieu_nhap.update_attributes(params[:phieu_nhap])
        format.html { redirect_to @home, notice: 'Param was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @phieu_nhap.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /phieu_nhaps/1
  # DELETE /phieu_nhaps/1.json
  def destroy
    @phieu_nhap = PhieuNhap.find(params[:id])
    @phieu_nhap.destroy

    respond_to do |format|
      format.html { redirect_to phieu_nhaps_url }
      format.json { head :no_content }
    end
  end

  def receipt_note
  end
end
